//
//  WZDetailSceduleController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/6/16.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZDetailSceduleController.h"
#import "WZAddScheduleController.h"
#import "CustomPopOverView.h"
#import "WZDetailSchedule.h"
#import "WZEditController.h"
#import "JKAlertDialog.h"


#define DayPlan_DeleteSchedule_Remind @"确定删除该日程吗?"

@interface WZDetailSceduleController ()<CustomPopOverViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *remindImgView;
@property (weak, nonatomic) IBOutlet UILabel *remindLabel;
@property (nonatomic, strong) CustomPopOverView *popView;
@property (nonatomic, strong) WZDetailSchedule *detailSchedule;

@end

@implementation WZDetailSceduleController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupTableView];
    
    [self getDetailSchedule];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.popView dismiss];
}

- (void)setupTableView {
    
    self.view.backgroundColor = RGBCOLORWITH16COLOR(0xf5f5f5);
    self.navigationItem.title = @"日程详情";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"schedule_more" title:nil target:self sel:@selector(moreDetail:) titleFont:Font17 titleColor:WhiteColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, 0)];
    
    self.tableView.scrollEnabled = NO;
}



// popView
- (void)moreDetail:(UIButton *)sender {
    
    NSArray *menus = @[@"编辑日程", @"删除日程"];
    CustomPopOverView *popView = [[CustomPopOverView alloc] initWithBounds:CGRectMake(0, 0, 90, 44 * 2) titleMenus:menus];
    popView.containerBackgroudColor = WZRGBColor(44, 170, 253);
    [popView showFrom:sender alignStyle:CPAlignStyleRight];
    popView.delegate = self;
    self.popView = popView;

}


#pragma mark -- popView Datelegate 跳转日程编辑
- (void)popOverView:(CustomPopOverView *)pView didClickMenuIndex:(NSInteger)index {
    
    // 编辑
    if (0 == index) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"WZEditController" bundle:nil];
        WZEditController *editVC = [storyboard instantiateViewControllerWithIdentifier:@"edit"];
        
        editVC.detailSchedule = self.detailSchedule;
        editVC.oaUserName = self.oaUserName;
        editVC.oaPwd = self.oaPwd;
        
        editVC.block = ^(){
            
            [self getDetailSchedule];
            [self.tableView reloadData];
        };
        
        [self.navigationController pushViewController:editVC animated:YES];
    }
    
    // 删除
    if (1 == index) {
        
        JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:nil message:DayPlan_DeleteSchedule_Remind];
        [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
            
            [NetWorkShare deleteByIDWithUserName:self.oaUserName pwd:self.oaPwd scheduleID:self.scheduleID successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
                if (responseObject) {
                    
                    if ([[responseObject objectForKey:@"success"] boolValue]) {
                        [MBProgressHUD showSuccess:@"删除日程成功"];
                        WZLog(@"删除日程成功");
                        
                        [self.navigationController popViewControllerAnimated:YES];
                        
                        [self.tableView reloadData];
                        
                    } else {
                        
                        [MBProgressHUD showError:[responseObject objectForKey:@"success"]];
                    }
                } else {
                    
                    [MBProgressHUD showError:UNKNOWNERROR];
                }
            } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                
                [MBProgressHUD showError:NETWORKNERROR];
            }];
            
        }];
        
        [alert addButton:Button_CANCEL withTitle:@"取消" handler:^(JKAlertDialogItem *item) {
            
            
        }];
        
        [alert show];
        
        WZLog(@"scheduleID = %@", self.scheduleID);
    }
    
}


#pragma mark -- 日程详情
- (void)getDetailSchedule {
    
    [NetWorkShare getByIDWithUserName:self.oaUserName pwd:self.oaPwd scheduleID:self.scheduleID successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD showMessage:@"加载数据..."];
        if (responseObject) {
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [MBProgressHUD hideHUD];
                self.detailSchedule = [WZDetailSchedule objectWithKeyValues:responseObject[@"detail"]];
                
                if ([self.detailSchedule.typeFlag isEqualToString:@"2"]) {
                    
                    self.navigationItem.rightBarButtonItem = nil;
                }
                
                [self loadDetailScheduleData];
                
                [self.tableView reloadData];
            
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
        } else {
            [MBProgressHUD showError:UNKNOWNERROR];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [MBProgressHUD showError:NETWORKNERROR];
        WZLog(@"failed = %@",error);
    }];
    
}


/**加载日程详情数据*/
- (void)loadDetailScheduleData {
    
    self.titleLabel.text = self.detailSchedule.title;
    
    NSArray *timeArray = [NSDate separteTimeStr:self.detailSchedule.startTime sepStr:@" "];
    self.dateLabel.text = timeArray.firstObject;
    self.timeLabel.text = timeArray.lastObject;
    
    if ([self.detailSchedule.alertFlag isEqualToString:@"1"]) {
        
        self.remindImgView.image = [UIImage imageNamed:@"alarm---bell"];
        self.remindLabel.text = @"不提醒";
    } else {
        self.remindLabel.text = [NSString stringWithFormat:@"提前%@%@", self.detailSchedule.beforeLength, self.detailSchedule.beforeUnitName];
    }
}

@end
